package cn.itsy.web.controller;

import cn.itsy.pojo.ResultPojo;
import cn.itsy.pojo.Warnrule;
import cn.itsy.service.warnrule.WarnRuleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description:
 * @author: MisterGay
 * @date: 2019-06-03 16:00
 */
@Controller
@RequestMapping(value = "warnRule")
public class WarnRuleController {
    @Resource(name = "warnRuleServiceimpl")
    private WarnRuleService warnRuleService;

    /**
     * 查询所有预警规则
     *
     * @return
     */
    @RequestMapping("queryWarnruleAll")
    @ResponseBody
    public List<Warnrule> queryWarnruleAll() {
        String sql = "select warnrule.*,warntype.wtname from warnrule,warntype,emp where warntype.id=warntype_id and emp.id=warnrule.emp_id and warnrule.status='Y' ";
        List<Warnrule> warnrules = this.warnRuleService.queryWarnruleAll(sql);
        return warnrules;
    }

    /**
     * 添加预警规则
     *
     * @return
     */
    @RequestMapping("insertWarnrule")
    @ResponseBody
    public void insertWarnrule(Warnrule warnrule) {
        String sql = "insert into warnrule values(?,?,?,?,?,?,?,?,?,?,?,?)";
        this.warnRuleService.insertWarnrule(sql, warnrule);
    }

    /**
     * 修改预警规则
     */
    @RequestMapping("updateWarnrule")
    @ResponseBody
    public void updateWarnrule(Warnrule warnrule) {
        String sql = "update warnrule set wrname=?,warntype_id=?,wdatenumber=?,depts=? where id=?";
        this.warnRuleService.updateWarnrule(sql, warnrule);
    }

    /**
     * 删除预警规则
     */
    @RequestMapping("deleteWarnruleById")
    @ResponseBody
    public void deleteWarnrule(Warnrule warnrule) {
        System.out.println(warnrule.toString());
        String sql = "update warnrule set status='N' where id=? ";
        this.warnRuleService.deleteWarnrule(sql, warnrule);
    }

    /**
     * 执行预警规则
     */
    @RequestMapping("exqWarnruleById")
    @ResponseBody
    public List<ResultPojo> exqWarnruleById(Integer id) {
        String sql = "select emp.id,emp.empid,emp.empname,dept.dname,job.name,warnrule.wrname " +
                " from emp,warnrule,dept,job " +
                " where emp.id=warnrule.emp_id " +
                " and emp.depart_id=dept.id " +
                " and emp.job_id=job.id " +
                " and warnrule.id=? ";
        List<ResultPojo> resultPojos = this.warnRuleService.queryResultPojoById(sql, id);
        return resultPojos;
    }
}
